﻿// *************************************************************************************************
// Project: RealSense.Light.AutoFac
// Namespace: RealSense.Light.AutoFac
// File: bootstrapper.cs  Author: Peter O'Hanlon
// Copyright © Peter O'Hanlon 2014-2015
// Date: 2015/1/4 12:53
// *************************************************************************************************
using Autofac;
using RealSense.Light.Contracts;
using RealSense.Light.Core;
using RealSense.Light.Extensions;
using RealSense.Light.Extensions.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace RealSense.Light.AutoFac
{
  public class Bootstrapper : Application, IBootstrapper
  {
    protected override void OnStartup(StartupEventArgs e)
    {
      base.OnStartup(e);

      Build();
    }

    public static IContainer Container { get; private set; }

    private void Build()
    {
      if (Container == null)
      {
        var builder = new ContainerBuilder();
        Configure(builder);
        Container = builder.Build();

        Resolver = Container.Resolve<IResolver>();
        ResolutionContext.SetResolver(Resolver);
      }
    }

    protected virtual void Configure(ContainerBuilder builder)
    {
      builder.RegisterType<Session>().As<ISession>().SingleInstance();
      builder.RegisterType<Resolver>().As<IResolver>().SingleInstance();
      builder.RegisterType<RealSenseApplication>().As<IApplication>().SingleInstance();
      builder.RegisterType<RangeValidation>().As<IRangeValidation>();
      // Speech synthesis registration.
      builder.RegisterType<SpeechSynthesisDevices>().As<ISpeechSynthesisDevices>();
      builder.RegisterType<SpeechSynthesizerValidation>().As<ISpeechSynthesizerValidation>().SingleInstance();
      builder.RegisterType<SpeechSynthesizer>().As<ISpeechSynthesizer>();

      builder.RegisterType<SpeechSynthesizerBehavior>();
    }

    public IResolver Resolver
    {
      get;
      private set;
    }
  }
}